package com.gdcn.report.buider;

import java.sql.Connection;
import java.util.List;

import com.gdcn.report.data.FileData;
import com.gdcn.report.service.ReportDataService;

public class MultipleXMLBuilder extends Builder {
	StringBuffer chartXML = new StringBuffer();
	
	String caption;
	
	List<String> data_x;
	List<List<String>> data_y;
	List<String> seriesName;
	List<String> parentYAxis;
	
	List<String> colors;

	String PYAxisName;
	String SYAxisName;
	String xAxisName;

	@Override
	protected void buildBody() {
		buildCategories();
		buildDataset();
	}
	
	private void buildCategories() {
		chartXML.append("<categories>");
		for(String str : data_x) {
			if(null != str && !"".equals(str)) {
				chartXML.append("<category name='").append(str).append("' />");
			}
		}
		chartXML.append("</categories>");
	}
	
	private void buildDataset() {
		int i=0;
		for(List<String> list : data_y) {
			if("P".equals(parentYAxis.get(i))) {
				chartXML.append("<dataset seriesName='").append(seriesName.get(i)).append("' parentYAxis='").append("P").append("' color='").append(colors.get(i)).append("' numberPrefix='$'>");
			} else {
				chartXML.append("<dataset seriesName='").append(seriesName.get(i)).append("' parentYAxis='S' anchorSides='10' anchorRadius='3' >");
			}
			for(String str : list) {
				chartXML.append("<set value= '").append(str).append("' />");
			}
			chartXML.append("</dataset>");
			i++;
		}
	}

	@Override
	protected void buildHeader() {
		chartXML.append("<graph caption='").append(caption).append("' showValues='0").append("' PYAxisName='").append(PYAxisName).append("' SYAxisName='").append(SYAxisName).append("' xAxisName='").append(xAxisName).append("' >");
	}

	@Override
	protected String buildTail() {
		chartXML.append("</graph>");System.out.println(chartXML.toString());
		return chartXML.toString();
	}

	public List<String> getData_x() {
		return data_x;
	}

	public void setData_x(List<String> data_x) {
		this.data_x = data_x;
	}

	public List<List<String>> getData_y() {
		return data_y;
	}

	public void setData_y(List<List<String>> data_y) {
		this.data_y = data_y;
	}

	public List<String> getSeriesName() {
		return seriesName;
	}

	public void setSeriesName(List<String> seriesName) {
		this.seriesName = seriesName;
	}

	public List<String> getParentYAxis() {
		return parentYAxis;
	}

	public void setParentYAxis(List<String> parentYAxis) {
		this.parentYAxis = parentYAxis;
	}

	public String getPYAxisName() {
		return PYAxisName;
	}

	public void setPYAxisName(String axisName) {
		PYAxisName = axisName;
	}

	public String getSYAxisName() {
		return SYAxisName;
	}

	public void setSYAxisName(String axisName) {
		SYAxisName = axisName;
	}

	public String getXAxisName() {
		return xAxisName;
	}

	public void setXAxisName(String axisName) {
		xAxisName = axisName;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	@Override
	public void init(ReportDataService reportService, Connection connection,
			FileData fd) {
		this.setParentYAxis(fd.getParent_yAxis());
		this.setSeriesName(fd.getSeriesName());
		this.setData_x(reportService.query_x(connection, fd));
		this.setData_y(reportService.query_y(connection, fd));
		this.setCaption(fd.getCaption());
		this.setXAxisName(fd.getXAxisName());
		this.setPYAxisName(fd.getPyAxisName());
		this.setSYAxisName(fd.getSyAxisName());
		this.setColors(fd.getDisplayColor());
	}

	public List<String> getColors() {
		return colors;
	}

	public void setColors(List<String> colors) {
		this.colors = colors;
	}

}
